<!DOCTYPE html>
<html xmlns:th="http://www.springframework.org/schema/data/jaxb" >
<head>
    <meta charset="UTF-8">
    <title>菜单管理</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" th:href="@{../lib/layui/css/layui.css}">
    <link rel="stylesheet" th:href="@{../css/okadmin.css}">
    <link rel="stylesheet" th:href="@{../font/iconfont.css}">
    <link rel="stylesheet" th:href="@{../lib/nprogress/nprogress.css}">
</head>
<body>
<div class="ok-body">
    <okToolbar>
        <button class="layui-btn layui-btn-sm" id="addMenu">
            <i class="layui-icon">&#xe61f;</i>新增菜单
        </button>
        <a class="layui-btn layui-btn-sm ok-refresh" href="javascript:location.replace(location.href);" title="刷新">
            <i class="layui-icon layui-icon-refresh"></i>
        </a>
    </okToolbar>
    <!--数据表格-->
    <table class="layui-hide" id="tableId" lay-filter="tableFilter"></table>
</div>
<!--js逻辑-->
<script th:src="@{../lib/layui/layui.js}"></script>
<script th:src="@{../lib/nprogress/nprogress.js}"></script>
<script>
    NProgress.start();
    window.onload = function () {
        NProgress.done();
    }

    layui.config({
        base: '../lib/layui_plugins/'
    }).extend({
        treetable: 'treetable/treetable'
    }).use(['element', 'table', 'form', 'jquery', 'laydate', 'treetable'], function () {
        var form = layui.form;
        var table = layui.table;
        var $ = layui.jquery;
        var treetable = layui.treetable;

        layer.load(2);

        var renderTable = function (){
            treetable.render({
                id : 'tableId',
                treeColIndex: 1,
                treeSpid: -1,
                treeIdName: 'id',
                treePidName: 'pId',
                elem: '#tableId',
                url: 'treeTable',
                skin: 'line', //行边框风格
                even: true, //开启隔行背景
                size: 'sm', //小尺寸的表格
                page: false,
                cols: [[
                    {type: 'radio', id : 'id', hide : true},
                    {field : 'entity.name', minWidth: 200, title: '菜单名称'},
                    {title: '请求路径', templet: function (d) {
                            if(d.entity.url == null){
                                return '';
                            }
                            return d.entity.url;
                        }
                    },
                    {field: 'entity.icon', title: '图标'},
                    {templet: '#operationTpl', width: 120, align: 'center', title: '操作'}
                ]],
                done: function (res, curr, count) {
                    layer.closeAll('loading');
                    treetable.foldAll("#tableId");
                    $("#countNum").text(count);
                }
            });
        }

        renderTable();

        form.on('submit(search)', function () {
            layer.msg('正在查询，请稍后...');
            return false;
        });

        table.on('tool(tableFilter)', function (obj) {
            var data = obj.data;
            var layEvent = obj.event;
            if (layEvent === 'editMenu') {
                layer.open({
                    title: '编辑权限',
                    type: 2,
                    shade: false,
                    maxmin: true,
                    shade: 0.5,
                    area: ['50%', '90%'],
                    content: 'editPage?id=' + data.id,
                    zIndex: layer.zIndex,
                    end: function () {
                        renderTable();
                        $(".layui-laypage-btn")[0].click();
                    }
                });
            } else if (layEvent === 'del') {
                layer.confirm("确定要删除[" + data.entity.name + "]吗？", {skin: 'layui-layer-lan', icon: 2, title: '提示', anim: 6}, function () {
                    $.ajax({
                        url : "delete?id=" + data.id,
                        method : "get",
                        dataType : "json",
                        success : function (result) {
                            if(result.success){
                                layer.msg("操作成功！", {icon: 1, time: 1000}, function () {
                                    renderTable();
                                });
                            }else{
                                layer.msg("删除失败, 稍后重试!", {icon: 1, time: 1000});
                            }
                        },
                        error: function () {
                            layer.msg("删除失败, 稍后重试!", {icon: 1, time: 1000});
                        }
                    })
                });
            }
        });

        $("#addMenu").click(function () {
            var rows = treetable.selectRows("tableId").data;

            var pid = '';
            if(rows.length == 1){
                pid = rows[0].id;
            }

            layer.open({
                title: '新增菜单',
                type: 2,
                shade: false,
                maxmin: true,
                shade: 0.5,
                anim: 4,
                area: ['50%', '90%'],
                content: 'addPage?pid=' + pid,
                zIndex: layer.zIndex,
                success : function(layero, index){
                },
                end: function () {
                    renderTable();
                }
            });
        })
    })

</script>
<!--模板-->
<script type="text/html" id="operationTpl">
    <a href="javascript:;" class="ok-row-operate" title="编辑" lay-event="editMenu"><i class="layui-icon">&#xe642;</i></a>
    <a href="javascript:;" class="ok-row-operate" title="删除" lay-event="del"><i class="layui-icon">&#xe640;</i></a>
</script>
</body>
</html>